package com.test.servlet.user;

import com.mysql.jdbc.StringUtils;
import com.test.pojo.Role;
import com.test.pojo.User;
import com.test.service.RoleService;
import com.test.service.RoleServiceTmpl;
import com.test.service.UserService;
import com.test.service.UserServiceTmpl;
import com.test.util.Contents;
import com.test.util.PageSupport;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

// 实现servlet复用
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 根据字段复用
        String method = req.getParameter("method");
        if(method.equals("query") && method != null){
            this.userList(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 根据字段复用
        String method = req.getParameter("method");
        if(method.equals("savepwd") && method != null){
            this.updatePwd(req,resp);
        }
    }

    /**
     * 用户列表
     * @param req
     * @param resp
     */
    public void userList(HttpServletRequest req, HttpServletResponse resp){
        UserService userService = new UserServiceTmpl();
        RoleService roleService = new RoleServiceTmpl();
        int currentPageNo = 1;
        int pageSize = 2;
        String userName = null; //用户名称查询条件
        int userRole = 0;  //角色查询条件
        // 获取参数
        String page = req.getParameter("page");
        String queryUserName = req.getParameter("userName");
        String roleId = req.getParameter("roleId");
        if(queryUserName == null){
            userName = "";
        }
        if(roleId != null && !roleId.equals("")){
            userRole = Integer.parseInt(roleId);
        }
        if(page != null && !page.equals("")){
            currentPageNo = Integer.parseInt(page);
        }
        //获取总数
        int total = userService.getUserCount(userName,userRole);
        //总页数支持
        PageSupport pageSupport = new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(total);
        //控制和尾页
        int totalPageCount = pageSupport.getTotalPageCount(); //总页数
        if(totalPageCount<1){
            currentPageNo = 1;
        }else if(totalPageCount>totalPageCount){
            currentPageNo =totalPageCount;
        }
        //查询用户列表
        List<User> userList = userService.getUserList(userName,userRole,currentPageNo,pageSize);
        //查询角色类表
        List<Role> roleList = roleService.getRoleList();
        // 转发 传数据
        req.setAttribute("userList",userList);
        req.setAttribute("roleList",roleList);
        req.setAttribute("currentPageNo",currentPageNo); //当前页数
        req.setAttribute("totalCount",total);  //总数据条数
        try {
            req.getRequestDispatcher("/jsp/userlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 修改用户密码
     * @param req
     * @param resp
     */
    public void updatePwd(HttpServletRequest req, HttpServletResponse resp){
        //
        HttpSession session = req.getSession();
        Object userSession = session.getAttribute(Contents.USER_SESSION);
        String rnewpassword = req.getParameter("rnewpassword");  //获取新密码
        boolean upResult = false; //修改成功失败标记
        if(userSession != null && !StringUtils.isNullOrEmpty(rnewpassword)){
            UserService userService = new UserServiceTmpl();
            upResult = userService.updatePw(((User)userSession).getId(),rnewpassword);
            if(upResult){
                //移除session
                req.getSession().removeAttribute(Contents.USER_SESSION);
                req.setAttribute("message","修改成功");
            }
            else{
                req.setAttribute("error","密码修改失败");
            }
        }else{
            // 参数错误
            req.setAttribute("error","参数错误");
        }
        try {
            req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
